Ext.define('Lms.rms.HolidayConfigPanel',{
	extend : 'Ext.panel.Panel',
	initComponent : function(){
		Ext.apply(this,{
			id : 'HolidayConfigPanel',
			layout : 'border',
			title : LocaleUtil.getMessage('rms.menu.holidayConf'),
			borad : false,
			items : [
			    this.getWeekdayGrid(),
			    this.getTempHolidayGrid()
			]
		});
		this.callParent(arguments);
	},
	
	getWeekdayGrid : function(){		
		var weekdayStore = Ext.create('Ext.data.Store',{
			fields:['id','weekDay', 'onYn', 'displayOnYn'],
			autoLoad : true,
		    proxy : {
		    	url : 'schedule/getRegList.do',
		    	type : 'ajax',
				reader : {
					type : 'json'
				}
		    }
		});
		this.weekDayGrid = Ext.create('Ext.grid.Panel',{
			region : 'center',
			title : LocaleUtil.getMessage('rms.label.regularHoliday'),
			width  : '50%',
			store : weekdayStore,
			hideHeaders : true,
			columnLines: true,
			columns : [
			    {xtype: 'checkcolumn', dataIndex : 'displayOnYn', width : 30},
			    {text : '',dataIndex : 'weekDay',  flex: 1 , renderer : function(value){
			    	if(value == 0) return LocaleUtil.getMessage('rms.label.everySunday');
			    	else if(value == 1) return LocaleUtil.getMessage('rms.label.everyMonday');
			    	else if(value == 2) return LocaleUtil.getMessage('rms.label.everyTuesday');
			    	else if(value == 3) return LocaleUtil.getMessage('rms.label.everyWednesday');
			    	else if(value == 4) return LocaleUtil.getMessage('rms.label.everyThursday');
			    	else if(value == 5) return LocaleUtil.getMessage('rms.label.everyFriday');
			    	else if(value == 6) return LocaleUtil.getMessage('rms.label.everySaturday');
			    }}
			],
			tbar : [{
				xtype : 'button',
				text : LocaleUtil.getMessage('save'),
				iconCls : 'save',
				scope : this,
				handler : this.onSaveHoliday
			}]
		});
		return this.weekDayGrid;
	},
	
	onSaveHoliday : function(){
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToSave'),function(btn){
			if(btn == 'yes'){
				var param = '?',
					i=0;
				this.weekDayGrid.getStore().each(function(record){
					param += 'scheduleVOs['+i+'].id='+record.get('id')+'&' +
					'scheduleVOs['+i+'].weekDay='+record.get('weekDay')+'&' +
		 	    	'scheduleVOs['+i+'].onYn='+(record.get('displayOnYn')?'N':'Y') +'&';
					i++;
				});
				
				Ext.Ajax.request({
			        url:'schedule/updateReg.do' + param,
			        scope : this,
			        success : function(result, request){
			        	this.weekDayGrid.getStore().reload();
			        },
			        failure: Common.failureMessage
			    });
			}
		},this);			
	},
	
	getTempHolidayGrid : function(){
		this.tempHolidayStore = Ext.create('Ext.data.Store',{
			fields : ['id','dateDay'],
			autoLoad : true,
			proxy : {
		    	url : 'schedule/getTempList.do',
		    	type : 'ajax',
				reader : {
					type : 'json'
				}
		    }
		});
		
		this.tempHolidayGrid = Ext.create('Ext.grid.Panel',{
			region : 'east',
			title : LocaleUtil.getMessage('rms.label.temporaryHoliday'),
			width  : '50%',
			store : this.tempHolidayStore,
			selType: 'checkboxmodel',
			hideHeaders : true,
			columnLines: true,
			columns : [
			    { text : '', dataIndex : 'dateDay', flex : 1 }
			],
			tbar : [{
					xtype : 'button',
					text : LocaleUtil.getMessage('save'),
					iconCls : 'save',
					scope : this,
					handler : this.onSaveTempHoliday
				},'->',{
					text : LocaleUtil.getMessage('rms.label.addTempHoliday'),
					iconCls : 'add',
					menu : Ext.create('Ext.menu.DatePicker',{
						scope : this,
						handler : this.onDateClickHandler,
						minDate : new Date()
					})
				},{
					xtype : 'button',
					text : LocaleUtil.getMessage('del'),
					iconCls : 'delete',
					scope : this,
					handler : function(){
						this.tempHolidayStore.remove(this.tempHolidayGrid.getSelectionModel().getSelection());
					}
				}
			]
		});
		return this.tempHolidayGrid;
	},
	
	onSaveTempHoliday : function(){
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToSave'),function(btn){
			if(btn == 'yes'){
				var newParam = '?',
				delParam = '?';
				var newRecords = this.tempHolidayGrid.getStore().getNewRecords();
				for(var i in newRecords){
					var record = newRecords[i];
					newParam += 'scheduleVOs['+i+'].dateDay='+record.get('dateDay')+'&';
				}
				if(newRecords.length > 0){
					Ext.Ajax.request({
						url:'schedule/createTemp.do' + newParam,
						scope : this,
						success : function(result, request){
							this.tempHolidayGrid.getStore().reload();
						},
						failure: Common.failureMessage
					});
				}
				var removedRecords = this.tempHolidayGrid.getStore().getRemovedRecords();
				for(var i in removedRecords){
					var record = removedRecords[i];
					delParam += 'scheduleVOs['+i+'].id='+record.get('id')+'&';
				}
				if(removedRecords.length > 0){
					Ext.Ajax.request({
						url:'schedule/deleteTempByIds.do' + delParam,
						scope : this,
						success : function(result, request){
							this.tempHolidayGrid.getStore().reload();
						},
						failure: Common.failureMessage
					});
				}
			}
		},this);
	},
	
	onDateClickHandler : function(dp, date){
		this.tempHolidayStore.add({dateDay : Ext.Date.format(date, 'Y-m-d')});
	}
});